package org.atovio.common.utils;

import org.atovio.common.exception.NormalException;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 工具类
 */
public class BusiUtil {

    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    public static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_PATTERN);

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_PATTERN);

    private static final ResourceBundle ERROR_BUNDLE = ResourceBundle.getBundle("ErrorCode");

    private BusiUtil() {
    }

    public static LocalDate string2Date(String dateStr) {
        if (isNull(dateStr)) {
            return null;
        }
        return LocalDate.parse(dateStr, DATE_FORMATTER);
    }

    public static String date2String(LocalDate localDate) {
        return isNull(localDate) ? "" : localDate.format(DATE_FORMATTER);
    }

    public static LocalDateTime string2DateTime(String dateTimeStr) {
        if (isNull(dateTimeStr)) {
            return null;
        }
        return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER);
    }

    public static String dateTime2String(LocalDateTime dateTime) {
        return isNull(dateTime) ? "" : dateTime.format(DATE_TIME_FORMATTER);
    }

    /**
     * 判断对象是否为空
     *
     * @param obj 对象
     * @return 是否为空
     */
    public static boolean isNull(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof List<?>) {
            return ((List<?>) obj).isEmpty();
        } else if (obj instanceof Map<?, ?>) {
            return ((Map<?, ?>) obj).isEmpty();
        } else if (obj instanceof String) {
            return ((String) obj).isBlank();
        }
        return false;
    }

    /**
     * 判断对象是否非空
     *
     * @param obj 对象
     * @return 是否非空
     */
    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    public static boolean existNull(Object... objects) {
        return Arrays.stream(objects).anyMatch(BusiUtil::isNull);
    }

    public static boolean existNotNull(Object... objects) {
        return Arrays.stream(objects).anyMatch(BusiUtil::isNotNull);
    }

    public static boolean isNullAll(Object... objects) {
        return Arrays.stream(objects).allMatch(BusiUtil::isNull);
    }

    public static boolean isNotNullAll(Object... objects) {
        return Arrays.stream(objects).allMatch(BusiUtil::isNotNull);
    }

    public static boolean isEquals(Object obj1, Object obj2) {
        return Objects.equals(obj1, obj2);
    }

    public static boolean isNotEquals(Object obj1, Object obj2) {
        return !isEquals(obj1, obj2);
    }

    public static NormalException createNormalException(String code) {
        return new NormalException(ERROR_BUNDLE.getString(code));
    }

    public static NormalException createNormalException(String code, String... vars) {
        String bundleMsg = ERROR_BUNDLE.getString(code);
        String finalMsg = MessageFormat.format(bundleMsg, vars);
        return new NormalException(finalMsg);
    }
}
